# -*- coding: utf-8 -*-
from core.serializers import CustomModelSerializer
from core.relations import BaseNameWritableRelatedField, UserNameWritableRelatedField
from rest_framework import serializers
from .models import TestPlan


class ListTestPlanSerializer(CustomModelSerializer):
    """
    测试计划-列表序列化器
    """

    product = BaseNameWritableRelatedField()
    version = BaseNameWritableRelatedField()
    owner = UserNameWritableRelatedField()
    reqs_count = serializers.SerializerMethodField()
    cases_count = serializers.SerializerMethodField()

    def get_reqs_count(self, instance):
        return len(instance.requirements.all())

    def get_cases_count(self, instance):
        return len(instance.testcases.all())

    class Meta:
        model = TestPlan
        fields = "__all__"
        read_only_fields = ["id"]


class TestPlanSerializer(CustomModelSerializer):
    """
    测试计划-查询、新增、修改序列化器
    """

    product = BaseNameWritableRelatedField()
    version = BaseNameWritableRelatedField()
    owner = UserNameWritableRelatedField()
    requirements = BaseNameWritableRelatedField(
        required=False, allow_empty=True, many=True
    )
    testcases = BaseNameWritableRelatedField(
        required=False, allow_empty=True, many=True
    )

    class Meta:
        model = TestPlan
        fields = "__all__"
        read_only_fields = ["id"]
